package red.mlz.consoleconsumer.controller.category;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import red.mlz.common.entity.user.User;
import red.mlz.common.utils.BaseUtils;
import red.mlz.common.utils.Response;
import red.mlz.consoleconsumer.annotations.VerifiedUser;
import red.mlz.consoleconsumer.feign.category.CategoryFeign;

import java.util.Map;

@RestController
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryFeign categoryFeign;

    @RequestMapping("/category/tree")
    public Response getCategoryTree(@VerifiedUser User loginUser){
        if (BaseUtils.isEmpty(loginUser)){
            return new Response(1002);
        }
        Map<String, Object> result = categoryFeign.getCategoryTree();

        return new Response(1001,result);
    }

    @RequestMapping("/category/add")
    public Response addCategory(@VerifiedUser User loginUser,
                                @RequestParam(name = "categoryId",required = false)Long categoryId,
                                @RequestParam(name = "name")String name,
                                @RequestParam(name = "image")String image,
                                @RequestParam(name = "parentId",required = false )Long parentId) {
        if (BaseUtils.isEmpty(loginUser)){
            return new Response(1002);
        }
        try {
            Long resultCategoryId = categoryFeign.edit(categoryId, name, image, parentId);
            return new Response<>(1001, resultCategoryId);
        }catch (Exception e) {
            return new Response(4004,e.getMessage());
        }
    }

    @RequestMapping ("/category/update")
    public Response updateCategory(@VerifiedUser User loginUser,
                                   @RequestParam(name = "categoryId")Long categoryId,
                                   @RequestParam(name = "name")String name,
                                   @RequestParam(name = "image")String image,
                                   @RequestParam(name = "parentId" ,required = false)Long parentId) {
        if (BaseUtils.isEmpty(loginUser)){
            return new Response(1002);
        }
        try {
            Long resultCategoryId = categoryFeign.edit(categoryId, name, image,parentId);
            return new Response(1001,resultCategoryId);
        }catch (Exception e) {
            return new Response(4004,e.getMessage());
        }
    }

    @RequestMapping("/category/delete")
    public Response deleteCategory(@VerifiedUser User loginUser,
                                   @RequestParam(name = "categoryId") Long categoryId){
        if (BaseUtils.isEmpty(loginUser)){
            return new Response(1002);
        }

        try {
            if (categoryId != null) {
                // 删除游戏
                Integer isDeleted = categoryFeign.deleteCategory(categoryId);
                if (isDeleted == 0) {
                    return new Response<>(4004,"未找到对应分类，删除失败");
                }
            }else {
                return new Response<>(4004,"请输入有效分类Id");
            }
            return new Response<>(1001,"分类删除成功");
        }catch (Exception e) {
            return new Response(4004,"删除过程中发生错误: " + e.getMessage());
        }
    }
}
